package com.gourd.common.kafka.consumer;

import com.gourd.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author gourd
 */
@Component
@Slf4j
@ConditionalOnProperty(name = "spring.kafka.gourd.enabled", havingValue = "true")
public class KafkaConsumer {

    @KafkaListener(topics = {CommonConstant.TOPIC_ONE}, containerFactory = "kafkaListenerContainerFactory")
    public void kafkaConsumerTest(String msg) {
        log.info("^o^= 接收到消息--" + msg);
    }

    @KafkaListener(topics = {CommonConstant.TOPIC_TWO}, containerFactory = "kafkaListenerContainerFactory")
    public void listenPartition(List<ConsumerRecord<String, String>> records) {
        System.out.println(records.size());
        for (ConsumerRecord<String, String> consumerRecord : records){
            String value = consumerRecord.value();
            log.info("^o^= 消息：partition " + consumerRecord.partition() + " value " + consumerRecord.value() );
        }
    }
}
